<template>
    <div class="update-birthday-container">
        <van-datetime-picker
            v-model="currentDate"
            type="date"
            title="选择出生日期"
            :min-date="minDate"
            :max-date="maxDate"
            @confirm="onConfirm"
            @cancel="$emit('close')"
        />
    </div>
</template>

<script>
import { editUserProfile } from '@/api/user'
import dayjs from 'dayjs'
    export default {
        name: 'updateBirthday',
        props:{
            value:{
                type: String,
                required: true
            }
        },
        data() {
            return {
                minDate: new Date(1990, 0, 1),
                maxDate: new Date(),
                currentDate: this.value
            }
        },
        methods: {
            async onConfirm(){
                this.$toast.loading({
                    message: '保存中...',
                    forbidclick: true //禁止点击背景
                })

                const date = dayjs(this.currentDate).format("YYYY-MM-DD")

                await editUserProfile({
                    birthday: date
                }) 

                this.$toast.success('修改成功')
                // 修改成功更新组件的name -> 关闭弹框
                this.$emit('input', date)
                // this.$emit('input', this.localName)
                this.$emit('close')
            }
        },
    }
</script>

<style lang="less" scoped>

</style>